// =====================================================================================
// 
//       Filename:  rational.cpp
// 
//    Description:  
// 
//        Version:  1.0
//        Created:  21.10.2011 14:33:20
//       Revision:  none
//       Compiler:  g++
// 
//         Author:  Dmitriy Serdyuk (), serdyuk.dmitriy@gmail.com
//        Company:  
// 
// =====================================================================================

#include "rational.h"


Rational::Rational(int p, int q) : myP(p), myQ(q) {
}


    
Rational operator+(const Rational& r1, const Rational r2) {
    Rational tmp(0, 0);
    tmp.myP = r1.myP * r2.myQ + r2.myP * r1.myP;
    tmp.myQ = r1.myQ * r2.myQ;
    return tmp;
}

Rational operator-(const Rational& r1, const Rational r2) {
    Rational tmp(-r2.myP, r2.myQ);
    return r1 + tmp;
}

Rational operator*(const Rational& r1, const Rational r2) {
    Rational tmp(0, 0);
    tmp.myP = r1.myP * r2.myP;
    tmp.myQ = r1.myQ * r2.myQ;
    return tmp;
}

Rational operator/(const Rational& r1, const Rational r2) {
    Rational tmp(0, 0);
    tmp.myP = r1.myP * r2.myQ;
    tmp.myQ = r1.myQ * r2.myP;
    return tmp;
}

Rational operator+=(Rational& r1, const Rational& r2) {
    r1 = r1 + r2;
    return r1;
}

Rational operator-=(Rational& r1, const Rational& r2) {
    r1 = r1 - r2;
    return r1;
}

Rational operator/=(Rational& r1, const Rational& r2) {
    r1 = r1 / r2;
    return r1;
}

Rational operator*=(Rational& r1, const Rational& r2) {
    r1 = r1 * r2;
    return r1;
}

bool operator==(const Rational& r1, const Rational& r2) {
    Rational tmp(r1 - r2);
    return tmp.myP == 0;
}

bool operator!=(const Rational& r1, const Rational& r2) {
    return !(r1 == r2);
}

bool operator<=(const Rational& r1, const Rational& r2) {
    return (r1 < r2) || (r1 == r2);
}

bool operator>=(const Rational& r1, const Rational& r2) {
    return (r2 < r1) || (r1 == r2);
}

bool operator<(const Rational& r1, const Rational& r2) {
    Rational tmp(r1 - r2);
    return tmp.myP < 0;
}

bool operator>(const Rational& r1, const Rational& r2) {
    return r2 < r1;
}

Rational::operator double() {
    return ((double)myP) / myQ;
}

Rational operator++(Rational& r) {
    Rational tmp(1);
    //operator+=(r, tmp);
    r = r + tmp;
    return tmp;
}

Rational Rational::operator++(int) {
    Rational tmp(*(this));
//    (*(this)) += 1;
    Rational one(1);
    (*(this)) = (*(this)) + one;
    return tmp;
}
